package com.example.service.impl;

import com.example.common.ServiceResponse;
import com.example.common.TableServerResponse;
import com.example.mapper.ParkingMapper;
import com.example.pojo.Parking;
import com.example.service.IParkingEsService;
import com.example.service.IParkingService;
import com.example.service.IPropertyService;
import com.example.vo.ParkingListVO;
import com.example.vo.PropertyListVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.UUID;

/**
 * @BelongsProject: gxcwhd
 * @BelongsPackage: com.example.service.impl
 * @Author: LJ
 * @CreateTime: 2020-08-17 11:44
 * @Description:
 */
@Service
@Transactional
public class ParkingServiceImpl implements IParkingService {

    @Autowired
    ParkingMapper parkingMapper;

    @Autowired
    IParkingEsService parkingEsService;

//    @Autowired
//    RabbitTemplate rabbitTemplate;

    @Override
    @Transactional(readOnly = true)
    public TableServerResponse getParkingInfo(Integer page, Integer limit) {
        //1.开启分页查询
        PageHelper.startPage(page, limit);
        //page = (page - 1)*pageSize;
        //2.执行查询操作
        List<ParkingListVO> list = parkingMapper.getParkingList();
        //3.取出总共有多少条数据,使用分页实体类来取
        PageInfo pageInfo = new PageInfo(list);
        return TableServerResponse.createSuccess(0, list, pageInfo.getTotal());

    }

    @Override
    public ServiceResponse verify(ParkingListVO parkingListVO) {

        Integer integer = parkingMapper.verify(parkingListVO);
        if (integer >0){
            //修改成功
            return ServiceResponse.createSuccess(parkingListVO);
        }else{
            //修改失败
            return ServiceResponse.createError();
        }
    }

    @Override
    @Transactional(readOnly = true)
    public TableServerResponse searchParking(ParkingListVO parkingListVO) {

        List<ParkingListVO> list = parkingMapper.searchParking(parkingListVO);
        return TableServerResponse.createSuccess(list);
    }

//    @Override
//    public ServiceResponse addEsParking(Parking parking) {
//        //给订单添加uuid主键
//        parking.setParkingId(UUID.randomUUID().toString().replaceAll("-",""));
//
//        //先写入到mysql  成功后 给mq发消息
//        Integer integer = parkingMapper.addEsParking(parking);
//
//        if (integer == 1){
//            //如果添加成功 给mq发送一条消息 "订单添加消息"
//            //消息格式 "主键-操作类型"  "123456-insert"
//            rabbitTemplate.convertAndSend("es.mysql.copy.data",parking.getParkingId()+ "-insert");
//        }
//        return ServiceResponse.createSuccess();
//    }

//    @Override
//    @Transactional(readOnly = true)
//    public Parking getParkingById(String parkingId) {
//        return parkingMapper.getParkingById(parkingId);
//    }
//
//    @Override
//    public Integer deleteParking(String id) {
//        return parkingMapper.deleteParking(id);
//    }
//
//    @Override
//    public Integer updateParking(Parking parking) {
//        //先修改数据库
//        Integer integer = parkingMapper.updateParking(parking);
//        if (integer == 1){
//            rabbitTemplate.convertAndSend("es.mysql.copy.data",parking.getParkingId() + "-update");
//        }
//
//        return 1;
//    }

//    @Override
//    @Transactional(readOnly = true)
//    public TableServerResponse searchEsParking(Parking parking) {
//        Page<Parking> parkingPage = parkingEsService.findAll(PageRequest.of(0, 5));
//        return TableServerResponse.createSuccess(parkingPage);
//    }

//    @Override
//    public ServiceResponse deleteParkingById(String propertyId) {
//
//        //先删除数据库
//        Integer integer = parkingMapper.deleteParking(propertyId);
//        if (integer == 1) {
//            //数据库删除成功 给mq发消息
//            rabbitTemplate.convertAndSend("es.mysql.copy.data",propertyId+"-deldete");
//
//        }
//        return ServiceResponse.createSuccess();
//    }


}
